//
//  CommentTableViewCell.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/27.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "CommentTableViewCell.h"

@implementation CommentTableViewCell{
    
    UIImageView *avatarImageView;
    UILabel *nicknameLabel;
    UILabel *timeLabel;
    UILabel *commentLabel;
    UIView *divideLine;
    
    UIView *imageArea;
    NSMutableArray *imagesArray;
    
}


- (void)setComment:(CommentModel *)comment {
    
    [self->avatarImageView sd_setImageWithURL:[NSURL URLWithString:comment.user.avatar] placeholderImage:[UIImage imageNamed:@"avatar"] options:SDWebImageRefreshCached];
    self->nicknameLabel.text = comment.user.nickname;
    

    self->timeLabel.text = [Tool getFormatDate:comment.created_at];
    self->commentLabel.text = comment.content;

    if (comment.images.count == 0) {
        self->imageArea.hidden = YES;
        
        [divideLine makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->commentLabel.bottom).offset(10);
            make.bottom.equalTo(self.bottom).offset(0);
            make.left.equalTo(self.left).offset(10);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(1/[[UIScreen mainScreen] scale]);
        }];
        
    } else {
        self->imageArea.hidden = NO;
        
        [divideLine makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->imageArea.bottom).offset(10);
            make.bottom.equalTo(self.bottom).offset(0);
            make.left.equalTo(self.left).offset(10);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(1/[[UIScreen mainScreen] scale]);
        }];
    }
    
//    for (int i=0; i<4; i++) {
//        UIImageView * imageView = [self->imagesArray objectAtIndex:i];
//        imageView.image = nil;
//    }
    
    for (int i=0; i<comment.images.count; i++) {
        UIImageView * imageView = [self->imagesArray objectAtIndex:i];
        ImageModel *imageModel = [comment.images objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageModel.img_url] placeholderImage:[UIImage imageNamed:@"placeholder_1_1"] options:SDWebImageRefreshCached];
    }
}


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        self->avatarImageView = UIImageView.new;
        self->avatarImageView.layer.masksToBounds = YES;
        self->avatarImageView.layer.cornerRadius = 20;
        [self addSubview:self->avatarImageView];
        
        [self->avatarImageView makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(10);
            make.top.equalTo(self.top).offset(10);
            make.width.equalTo(40);
            make.height.equalTo(40);
        }];
        

        self->nicknameLabel = UILabel.new;
        self->nicknameLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:16];
        self->nicknameLabel.textColor = SECOND_FONT_COLOR;
        self->nicknameLabel.numberOfLines = 1;
        [self addSubview:self->nicknameLabel];

        [self->nicknameLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->avatarImageView.top).offset(3);
            make.left.equalTo(self->avatarImageView.right).offset(10);
        }];

        self->timeLabel = UILabel.new;
        self->timeLabel.font = [UIFont fontWithName:MAIN_FONT_NAME size:12];
        self->timeLabel.textColor = UIColorFromRGB(0x999999);
        self->timeLabel.numberOfLines = 1;
        [self addSubview:self->timeLabel];

        [self->timeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->nicknameLabel.bottom).offset(5);
            make.left.equalTo(self->nicknameLabel.left).offset(0);
        }];
        
        self->commentLabel = UILabel.new;
        self->commentLabel.numberOfLines = 0;
        self->commentLabel.font = [UIFont systemFontOfSize:16.0];
        self->commentLabel.preferredMaxLayoutWidth = MAIN_SCREEN_WIDTH - 70;
        self->commentLabel.textColor = MAIN_FONT_COLOR;
        [self addSubview:self->commentLabel];

        [self->commentLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->avatarImageView.bottom).offset(10);
            make.left.equalTo(self->nicknameLabel.left).offset(0);
            make.right.equalTo(self.right).offset(-10);
        }];

        
        int goodsImageWidth = (MAIN_SCREEN_WIDTH - 100)/4;
        
        self->imageArea = UIView.new;
        //self->imageArea.backgroundColor = [UIColor redColor];
        [self addSubview:self->imageArea];

        [self->imageArea mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->commentLabel.bottom).offset(0);
            make.left.equalTo(self->avatarImageView.right).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(goodsImageWidth + 10);
        }];


        self->imagesArray = NSMutableArray.new;
        UIImageView *lastView;
        for (int i = 0; i < 4; i++) {
            UIImageView *view = UIImageView.new;
            [self->imageArea addSubview:view];

            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self->imageArea.top).offset(10);
                make.left.equalTo(lastView ? lastView.right : self->imageArea.left).offset(10);
                make.width.equalTo(goodsImageWidth);
                make.height.equalTo(goodsImageWidth);
            }];

            lastView = view;
            [self->imagesArray addObject:view];
        }

        divideLine = UIView.new;
        divideLine.backgroundColor = [UIColor grayColor];
        [self addSubview:divideLine];

        [divideLine makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self->imageArea.bottom).offset(10);
            make.bottom.equalTo(self.bottom).offset(0);
            make.left.equalTo(self.left).offset(10);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(1/[[UIScreen mainScreen] scale]);
        }];

        
    }
    return self;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
